/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kolojavan;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.RandomAccessFile;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author hubert
 */
public class KoloJavaN extends JFrame {

    private JFrame okno;
    private JPanel form;
    private int count;
    //komponenty
    private JTable tabelaTable;
    private DefaultTableModel model;
    //private JTable tabelaContent;
    private JLabel lpLabel;
    JLabel imieLabel;
    JLabel nazwiskoLabel;
    JTextField lpTextfield;
    JTextField imieTextfield;
    JTextField nazwiskoTextField;
    JButton add;
    JButton save;

    public KoloJavaN() {
        count = 0;
        okno = new JFrame();
        okno.setLayout(new BorderLayout());
        String[] columnNames = new String[]{"Lp.", "Imię", "Nazwisko"};

        //inicjalizacja elementów
        tabelaTable = new JTable(new Object[][]{{null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}}, columnNames);

        //tabelaContent = new JTable(1, 3);
        //dodawanie elementów
        okno.add(tabelaTable.getTableHeader(), BorderLayout.NORTH);
        okno.add(tabelaTable, BorderLayout.CENTER);

        //inicjalizacja elementów
        lpLabel = new JLabel("Lp.:");
        lpTextfield = new JTextField();
        imieLabel = new JLabel("Imię:");
        imieTextfield = new JTextField();
        nazwiskoLabel = new JLabel("Nazwisko: ");
        nazwiskoTextField = new JTextField();
        add = new JButton("Dodaj");
        save = new JButton("Zapisz");
        //JPanel
        form = new JPanel();
        form.setLayout(new GridLayout(4, 2));
        form.add(lpLabel);
        form.add(lpTextfield);
        form.add(imieLabel);
        form.add(imieTextfield);
        form.add(nazwiskoLabel);
        form.add(nazwiskoTextField);
        form.add(add);
        form.add(save);
        //efrerr
        okno.add(form, BorderLayout.SOUTH);
        //koniec
        okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        okno.pack();
        okno.setVisible(true);
        
        //akcje przycisków
        add.addActionListener(al);
        add.setActionCommand("dodaj");
        save.addActionListener(al);
        save.setActionCommand("zapisz");
    }
    
    ActionListener al = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            switch(ae.getActionCommand()){
                case "dodaj":
                    addDataToTable();
                    break;
                case "zapisz":
                    System.out.print("save");
                    saveDataToFile();
                    break;
                default:
                    System.out.println("Dupadupa!");
            }
        }
    };

    void addDataToTable() {
        TableModel tmd = tabelaTable.getModel();
        tmd.setValueAt(lpTextfield.getText().toString(), count, 0);
        tmd.setValueAt(imieTextfield.getText().toString(), count, 1);
        tmd.setValueAt(nazwiskoTextField.getText().toString(), count, 2);
        if(count < 4) count++;
    }

    void saveDataToFile(){
        JFileChooser fc = new JFileChooser();
        if(fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
            File f = fc.getSelectedFile();
            try{
            RandomAccessFile plik = new RandomAccessFile(f, "rw");
            for(int i = 0; i < tabelaTable.getModel().getColumnCount(); i++){
                plik.writeBytes(tabelaTable.getColumnName(i).toString()+" ");
            }
            plik.writeBytes("\r\n");
            for(int i = 0; i < tabelaTable.getModel().getColumnCount(); i++){
                plik.writeBytes(tabelaTable.getModel().getValueAt(i, 0) +" ");
                plik.writeBytes(tabelaTable.getModel().getValueAt(i, 1) +" ");
                plik.writeBytes(tabelaTable.getModel().getValueAt(i, 2) +" ");
                plik.writeBytes("\r\n");
            }
            plik.close();
            JOptionPane.showMessageDialog(okno, "Zapis powiódł się!");
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        KoloJavaN tabela = new KoloJavaN();
    }
}